package com.tzxs.pay.main.bill.viewmodel;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.databinding.ObservableArrayList;
import android.databinding.ObservableBoolean;
import android.databinding.ObservableField;
import android.databinding.ObservableInt;
import android.support.annotation.NonNull;
import android.util.Log;


import com.tzxs.pay.main.bill.detail.model.MsgRefundBean;
import com.tzxs.pay.main.bill.detail.model.RefundBean;
import com.tzxs.pay.main.bill.detail.model.RelativeBean;
import com.tzxs.pay.main.bill.detail.model.VerifyOrderBean;
import com.tzxs.pay.main.bill.model.AutBeanDetail;
import com.tzxs.pay.main.bill.model.AutBillImp;
import com.tzxs.pay.main.bill.model.AutOrderBean;
import com.tzxs.pay.main.bill.model.CoInDetailBean;
import com.tzxs.pay.main.bill.model.CollectionBillBean;
import com.tzxs.pay.main.bill.model.CollectionBillEntity;
import com.tzxs.pay.main.bill.model.CountBean;
import com.tzxs.pay.main.bill.model.GtBean;
import com.tzxs.pay.main.bill.model.InBean;
import com.tzxs.pay.main.bill.source.BillDataSource;
import com.tzxs.pay.main.deposit.model.SettlementBean;
import com.tzxs.pay.main.first.model.NoticeBean;
import com.tzxs.pay.main.gather.model.ScanAutBean;
import com.tzxs.pay.main.gather.model.ScanPayBean;
import com.tzxs.pay.main.onlineaut.model.OnlineAutBean;
import com.tzxs.pay.main.onlineaut.model.OnlineAutPrint;
import com.tzxs.pay.main.onlineaut.model.OnlineAutSureInBean;
import com.tzxs.pay.main.onlineaut.model.OnlineDetailBean;
import com.tzxs.pay.main.onlineaut.model.ReSendMsgBean;
import com.tzxs.pay.util.SingleLiveEvent;
import com.tzxs.pay.util.netnavigator.SimpleNetNavigator;
import com.tzxs.pay.util.viewmodel.Callback;

import java.util.TreeMap;

/**
 * 账单viewmodel
 */
public class BillViewModel extends AndroidViewModel {

    private static final String TAG = BillViewModel.class.getSimpleName();
    public SingleLiveEvent<String> snackbarText = new SingleLiveEvent<>();
    public SingleLiveEvent<String> dialogText = new SingleLiveEvent<>();
    public SingleLiveEvent<String> depositText = new SingleLiveEvent<>();


    //结算成功订单号
    public ObservableField<SettlementBean.DataBean> settmentBean = new ObservableField<>();
    //获取的是否是预授权列表
    public ObservableBoolean isAut = new ObservableBoolean();
    //收款账单列表  分期账单用同一个列表
    public ObservableArrayList<CollectionBillBean> collectionData = new ObservableArrayList<>();
    //收款和分期搜索结果列表
    public ObservableArrayList<CollectionBillBean> collSearchData = new ObservableArrayList<>();
    //账单总bean
    public ObservableField<CollectionBillEntity.XDataBean> collBean = new ObservableField<>();

    //预授权账单
    public ObservableArrayList<AutBillImp> autData = new ObservableArrayList<>();


    //订单详情bean
    public ObservableField<CoInDetailBean.DataBean> coInBean = new ObservableField<>();
    public ObservableField<AutBeanDetail.DataBean> autDeatil = new ObservableField<>();
    public ObservableArrayList<RelativeBean> relativeOrderList = new ObservableArrayList<>();


    //首页统计数据
    public ObservableField<CollectionBillEntity.XDataBean.InBean> inBean = new ObservableField<>();

    //首页推送通知
    public ObservableField<NoticeBean> noticeBeans = new ObservableField<>();

    //扫码收款
    public ObservableField<ScanPayBean> scanPay = new ObservableField<>();
    //预授权扫码
    public ObservableField<ScanAutBean.DataBean> scanAutPayBean = new ObservableField<>();

    //退款bean
    public ObservableField<RefundBean> refundBean = new ObservableField<>();
    //短信验证码退款bean
    public ObservableField<MsgRefundBean.DataBean> msgRefundBean = new ObservableField<>();


    //在线预授权列表
    public ObservableArrayList<OnlineAutBean.DataBean> onlineList = new ObservableArrayList<>();

    //在线预授权item详情
    public ObservableField<OnlineDetailBean.DataBean> onlineAutDetail = new ObservableField<>();


    //短信预授权创建订单号
    public ObservableField<String> msgAutOrderId = new ObservableField<>();


    //获取打印数据
    public ObservableField<OnlineAutPrint.DataBean> printBean = new ObservableField<>();

    //在线预授权搜索结果
    public ObservableArrayList<OnlineAutBean.DataBean> searchOnlineResult = new ObservableArrayList<>();


    //判断订单是否超过退款期限
    public ObservableInt orderStatus = new ObservableInt();

    public BillViewModel(@NonNull Application application) {
        super(application);

    }


    //获取到所有收款账单  分期账单  只是type不同
    public void getCollectionBills(Callback callback, String type, String payType, int page, int number, String out_trade_no,
                                   String trade_no, String time_start, String time_end) {

        BillDataSource.getINSTANCE(getApplication()).getBills(new SimpleNetNavigator<CollectionBillEntity>(snackbarText, dialogText, callback) {
            @Override
            public String getTitle() {
                return "正在获取数据.....";
            }

            @Override
            public void success(CollectionBillEntity data) {

                if (data.getCode().equals("10001")) {
                    if (page == 1) {
                        if (collectionData != null) {
                            collectionData.clear();
                        }
                        collectionData.addAll(data.getData());
                    } else {
                        collectionData.addAll(data.getData());
                    }
                    callback.success();
                } else {
                    snackbarText.setValue(data.getMsg());
                }

            }

            @Override
            public void fail(String error) {
                snackbarText.setValue(error);
            }

        }, type, payType, page, number, out_trade_no, trade_no, time_start, time_end);

    }


    /**
     * 获取到单个账单详情
     *
     * @param tradeNum 账单号
     */
    public void getCollectionBillDetail(String tradeNum, Callback callback) {
        BillDataSource.getINSTANCE(getApplication()).getBillDetail(new SimpleNetNavigator<CoInDetailBean>(snackbarText, dialogText, callback) {
            @Override
            public String getTitle() {
                return "正在获取详情......";
            }

            @Override
            public void success(CoInDetailBean data) {
                if (data.getCode() == 10001) {
                    coInBean.set(data.getData().get(0));

                    if (data.getData().get(0).getRelationRecord() != null) {
                        relativeOrderList.addAll(data.getData().get(0).getRelationRecord());

                    }
                    callback.success();
                } else {
                    snackbarText.setValue(data.getMsg());
                }

            }

            @Override
            public void fail(String error) {
                snackbarText.setValue(error);
            }
        }, tradeNum);

    }

    //获取押金统计列表 获取预授权列表
    public void getDepositBills(boolean flag, String type, int page, int pageNum, String outOrderNo,
                                String remarks, String timeStart, String timeEnd, int payStatus, String typeTime, Callback callback) {

        Log.d(TAG, "getDepositBills: 查询预授权列表:outOrderNo :====" + outOrderNo + "remarks:====" + remarks + "timeStart:====" + timeStart + "timeEnd:===" + timeEnd);
        BillDataSource.getINSTANCE(getApplication()).getDepositBills(type,
                page, pageNum, outOrderNo, remarks, timeStart, timeEnd, payStatus, typeTime, new SimpleNetNavigator<AutOrderBean>(snackbarText, dialogText, callback) {
                    @Override
                    public String getTitle() {
                        return "正在获取数据...";
                    }

                    @Override
                    public void success(AutOrderBean data) {

                        isAut.set(true);


                        //将所有的列表添加到集合
                        if (flag) {//判断是否有有头部
                            if (page == 1) {
                                if (autData != null) {
                                    autData.clear();
                                }
                                autData.add(data.getX_data());
                                autData.addAll(autData.size(), data.getData());
                            } else {
                                autData.addAll(autData.size(), data.getData());
                            }
                        } else {//当不存在头部的时候
                            if (page == 1) {
                                if (autData != null) {
                                    autData.clear();
                                }
                                autData.addAll(data.getData());
                            } else
                                autData.addAll(autData.size(), data.getData());
                        }
                        Log.d(TAG, "success: 押金统计size:" + data.getData().size());
                        callback.success();
                    }

                    @Override
                    public void fail(String error) {

                        snackbarText.setValue(error);
                        Log.d(TAG, "fail: 获取数据失败：" + error);
                    }

                });
    }


    /**
     * 获取得到搜索收款
     * 和分期结果的列表
     */
    public void getCoInResultBills(String type, String payType, String note, int time_type, int page, int num, String outOrderNo,
                                   String startTime, String endTime, Callback callback) {
        Log.d(TAG, "getCoInResultBills: 此时的搜索条件：" + "type:" + type + "\nnote:" +
                note + "\ntime_type:" + time_type + "\nstartTime:" + startTime + "\nendTime:" + endTime);

        BillDataSource.getINSTANCE(getApplication()).getCollectionResultBills(type, payType, note, time_type,
                page, num,
                outOrderNo,
                startTime,
                endTime,
                new SimpleNetNavigator<CollectionBillEntity>(snackbarText, dialogText, callback) {
                    @Override
                    public String getTitle() {
                        return "正在加载...";
                    }

                    @Override
                    public void success(CollectionBillEntity data) {

                        isAut.set(false);
                        collBean.set(data.getX_data());
                        if (data.getCode().equals("10001")) {
                            if (page == 1) {
                                if (collSearchData != null) {
                                    collSearchData.clear();
                                }
                                collSearchData.addAll(data.getData());
                            } else {
                                collSearchData.addAll(data.getData());
                            }
                            callback.success();
                        } else if (data.getCode().equals("10002")) {
                            if (page == 1) {//当换条件查询时,没有数据 将列表清空
                                collSearchData.clear();
                            }
                            snackbarText.setValue(data.getMsg());
                        }


                    }
                });

    }


    /**
     * 结算
     *
     * @param aut_no   授权号
     * @param pay_num  消费金额
     * @param remarks  备注
     * @param callback 回调
     */
    public void normalSettlement(String aut_no, String pay_num, String remarks, Callback callback) {

        BillDataSource.getINSTANCE(getApplication()).normalSettlement(aut_no, pay_num, remarks, new SimpleNetNavigator<SettlementBean>(snackbarText, dialogText, callback) {
            @Override
            public String getTitle() {
                return "正在结算....";
            }


            @Override
            public void success(SettlementBean data) {
                if (data.getCode() == 90010) {
                    settmentBean.set(data.getData().get(0));
                    callback.success();
                    snackbarText.setValue("结算成功!");
                } else {
                    snackbarText.setValue(data.getMsg());
                }

            }

            @Override
            public void fail(String error) {
                snackbarText.setValue(error);
            }
        });

    }


    /**
     * 扫码收款
     *
     * @param remarks  备注
     * @param code     扫描结 果
     * @param amount   金额
     * @param desc     描述
     * @param callback 回调
     */
    public void scanPay(String remarks, String code, String amount, String desc, Callback callback) {
        BillDataSource.getINSTANCE(getApplication()).scanPay(remarks, code, amount, desc,
                new SimpleNetNavigator<ScanPayBean>(snackbarText, dialogText, callback) {
                    @Override
                    public String getTitle() {
                        return "正在收款....";
                    }

                    @Override
                    public void success(ScanPayBean data) {
                        scanPay.set(data);
                        if (data.getStatus() == 0) {
                            snackbarText.setValue(data.getMsg());
                            return;
                        }
                        snackbarText.setValue(data.getMsg());
                        callback.success();
                    }

                    @Override
                    public void fail(String error) {
                        snackbarText.setValue(error);
                    }
                });
    }

    /**
     * 扫码预授权
     *
     * @param remarks  备注
     * @param code     扫描结果
     * @param amount   金额
     * @param callback
     */

    public void scanAutPay(String remarks, String code, String amount, Callback callback) {

        BillDataSource.getINSTANCE(getApplication()).scanAutPay(remarks, code, amount,
                new SimpleNetNavigator<ScanAutBean>(snackbarText, dialogText, callback) {
                    @Override
                    public String getTitle() {
                        return "正在收款...";
                    }

                    @Override
                    public void success(ScanAutBean data) {
                        //请求数据成功

                        if (data.getCode() == 90004) {
                            snackbarText.setValue("收款成功!");
                            scanAutPayBean.set(data.getData().get(0));
                            callback.success();
                        }
                        else if (data.getCode() == 90002) {
                            snackbarText.setValue("此功能已被关闭");
                        }
                        else if (data.getCode() == 90006) {
                            snackbarText.setValue(data.getMsg());
                        } else {
                            depositText.setValue(data.getMsg());
                        }


                    }

                    @Override
                    public void fail(String error) {
                        snackbarText.setValue(error);
                    }
                });
    }

    /**
     * 获取预授权订单详情
     *
     * @param orderId  订单号
     * @param callback 回调
     */
    public void getAutBillDetail(String orderId, Callback callback) {
        BillDataSource.getINSTANCE(getApplication()).getAutBillDetail(orderId, new SimpleNetNavigator<AutBeanDetail>(snackbarText, dialogText, callback) {
            @Override
            public String getTitle() {
                return "正在获取订单详情....";
            }

            @Override
            public void success(AutBeanDetail data) {
                if (data.getCode() == 10001) {//成功
                    autDeatil.set(data.getData().get(0));
                    callback.success();
                } else {
                    snackbarText.setValue(data.getMsg());
                }
            }

            @Override
            public void fail(String error) {
                snackbarText.setValue(error);
            }
        });
    }


    /**
     * 退款
     *
     * @param orderId  订单号
     * @param bz       备注
     * @param password 支付密码
     * @param amount   金额
     * @param callback 回调
     */
    public void refund(String orderId, String bz, String password, String amount, Callback callback) {

        Log.d(TAG, "refund: 未超过时间退款的密码:+password:   " + password);
        BillDataSource.getINSTANCE(getApplication()).refund(orderId, bz, password, amount, new SimpleNetNavigator<RefundBean>(snackbarText, dialogText, callback) {
            @Override
            public String getTitle() {
                return "正在退款....";
            }

            @Override
            public void success(RefundBean data) {

                if (data.getStatus() == 1) {
                    refundBean.set(data);
                    callback.success();
                } else {//当退款超时的时候
                    snackbarText.setValue(data.getMsg());
                }
            }

            @Override
            public void fail(String error) {
                snackbarText.setValue(error);
            }
        });

    }

    /**
     * 获取首页的统计数据
     */
    public void getFirstPageData(Callback callback) {
        BillDataSource.getINSTANCE(getApplication()).getFirstPageData(new SimpleNetNavigator<CollectionBillEntity>(snackbarText, dialogText, callback) {
            @Override
            public String getTitle() {
                return null;
            }

            @Override
            public void success(CollectionBillEntity data) {
                if (data.getX_data() != null) {
                    inBean.set(data.getX_data().getIn());
                }
            }

            @Override
            public void fail(String error) {
                snackbarText.setValue(error);
            }
        });

    }


    /**
     * 获取在线预授权列表
     *
     * @param page     页码
     * @param num      数量
     * @param callback 回调
     */
    public void getOnlineAutList(int page, int num, Callback callback) {

        BillDataSource.getINSTANCE(getApplication()).getOnlineAutList(page, num,
                new SimpleNetNavigator<OnlineAutBean>(snackbarText, dialogText, callback) {
                    @Override
                    public String getTitle() {
                        return "正在获取数据...";
                    }

                    @Override
                    public void success(OnlineAutBean data) {
                        callback.success();
                        if (data.getCode() == 10001) {
                            if (page == 1) {
                                onlineList.clear();
                            }
                            onlineList.addAll(data.getData());
                        } else if (data.getCode() == 10002) {
                            snackbarText.setValue(data.getMsg());
                        }

                    }

                    @Override
                    public void fail(String error) {
                        snackbarText.setValue(error);
                    }
                });
    }


    /**
     * 获取在线预授权item详情
     *
     * @param orderId     订单号
     * @param printStatus 打印状态  1返回打印内容  0 不返回
     * @param callback    callback
     */
    public void getOnlineAutDetail(String orderId, String printStatus, Callback callback) {

        BillDataSource.getINSTANCE(getApplication()).getOnlineAutDetail(orderId, printStatus,
                new SimpleNetNavigator<OnlineDetailBean>(snackbarText, dialogText, callback) {
                    @Override
                    public String getTitle() {
                        return "正在获取数据...";
                    }

                    @Override
                    public void success(OnlineDetailBean data) {

                        if (data.getCode() == 10001) {
                            onlineAutDetail.set(data.getData().get(0));
                            callback.success();
                        } else {
                            snackbarText.setValue(data.getMsg());
                        }


                    }

                    @Override
                    public void fail(String error) {
                        snackbarText.setValue(error);
                    }
                });
    }

    /**
     * 获取在线预授权item详情打印信息
     *
     * @param orderId     订单号
     * @param printStatus 打印状态  1返回打印内容  0 不返回
     * @param callback    callback
     */
    public void getOnlineAutPrint(String orderId, String printStatus, Callback callback) {

        BillDataSource.getINSTANCE(getApplication()).getOnlineAutPrint(orderId, printStatus,
                new SimpleNetNavigator<OnlineAutPrint>(snackbarText, dialogText, callback) {
                    @Override
                    public String getTitle() {
                        return "正在获取数据...";
                    }

                    @Override
                    public void success(OnlineAutPrint data) {
                        if (data.getCode() == 10001) {
                            printBean.set(data.getData().get(0));
                            callback.success();
                        } else {
                            snackbarText.setValue(data.getMsg());
                        }
                    }

                    @Override
                    public void fail(String error) {
                        snackbarText.setValue(error);
                    }
                });
    }


    //创建短信预授权订单
    public void createMsgAut(float price, String bookingName, String bookingPhone, String receivePhone,
                             String bookingTime, String remarks, Callback callback) {
        BillDataSource.getINSTANCE(getApplication()).createMsgAut(price,
                bookingName, bookingPhone, receivePhone,
                bookingTime, remarks, new SimpleNetNavigator<ReSendMsgBean>(snackbarText, dialogText, callback) {
                    @Override
                    public String getTitle() {
                        return "正在创建订单...";
                    }

                    @Override
                    public void success(ReSendMsgBean data) {
                        if (data.getCode() == 10001) {
                            msgAutOrderId.set(data.getData().get(0).getOut_order_no());
                            callback.success();
                            Log.d(TAG, "success: 发送短信接口:成功");
                        } else {
                            Log.d(TAG, "success: 发送短信接口:报错");
                            snackbarText.setValue(data.getMsg());
                        }

                    }

                    @Override
                    public void fail(String error) {
                        snackbarText.setValue(error);
                        Log.d(TAG, "fail: 发送短信接口:报错");

                    }
                });
    }

    /**
     * 重新发送短信
     *
     * @param orderId  订单号
     * @param callback 回调
     */
    public void resendMsg(String orderId, Callback callback) {

        BillDataSource.getINSTANCE(getApplication()).resendMsg(orderId, new SimpleNetNavigator<ReSendMsgBean>(snackbarText, dialogText, callback) {
            @Override
            public String getTitle() {
                return "重新发送短信...";
            }

            @Override
            public void success(ReSendMsgBean data) {
                if (data.getCode() == 10001) {
                    snackbarText.setValue(data.getMsg());
                    callback.success();
                } else {
                    snackbarText.setValue(data.getMsg());
                }
            }

            @Override
            public void fail(String error) {
                snackbarText.setValue(error);
            }
        });
    }

    //短信预授权订单核销
    public void cancelOrder(String orderId, Callback callback) {

        BillDataSource.getINSTANCE(getApplication()).cancelMsgAut(orderId,
                new SimpleNetNavigator<OnlineAutSureInBean>(snackbarText, dialogText, callback) {
                    @Override
                    public String getTitle() {
                        return "正在核销订单...";
                    }

                    @Override
                    public void success(OnlineAutSureInBean data) {
                        if (data.getCode() == 10001) {
                            callback.success();
                            snackbarText.setValue(data.getMsg());
                        } else {
                            snackbarText.setValue(data.getMsg());
                        }
                    }

                    @Override
                    public void fail(String error) {
                        snackbarText.setValue(error);
                    }
                });
    }


    //获取在线预授权搜索结果列表
    public void getSearchAutsList(String traceNo, String orderId, String bookingName, String bookingPhone, String orderPhone,
                                  String startTime, String endTime, Callback callback) {
        BillDataSource.getINSTANCE(getApplication()).getSearchAutsList(traceNo, orderId, bookingName,
                bookingPhone, orderPhone, startTime, endTime,
                new SimpleNetNavigator<OnlineAutBean>(snackbarText, dialogText, callback) {
                    @Override
                    public String getTitle() {
                        return "正在获取数据....";
                    }

                    @Override
                    public void success(OnlineAutBean data) {
                        if (data.getCode() == 10001) {
                            searchOnlineResult.addAll(data.getData());
                            callback.success();
                        } else {
                            snackbarText.setValue(data.getMsg());
                        }
                    }

                    @Override
                    public void fail(String error) {
                        snackbarText.setValue(error);
                    }
                });
    }


    //开始推送消息
    public void startGt(String cid, String title, String content, Callback callback) {
        BillDataSource.getINSTANCE(getApplication()).startGt(cid, title, content,
                new SimpleNetNavigator<GtBean>(dialogText, snackbarText, callback) {
                    @Override
                    public String getTitle() {
                        return null;
                    }

                    @Override
                    public void success(GtBean data) {
                        if (data.getCode().equals("10001")) {
                            callback.success();
                        } else {
                            snackbarText.setValue(data.getMsg());
                            Log.d(TAG, "success: 个推 :====" + data.getMsg());
                        }
                    }

                    @Override
                    public void fail(String error) {
                        snackbarText.setValue(error);
                        Log.d(TAG, "fail: 个推 炸了:====" + error);
                    }
                });

    }

    // 超过退款时间 短信验证码退款
    public void startMsgRefund(String payPwd, String verifyStr, String orderId, String bz, Callback callback) {
        BillDataSource.getINSTANCE(getApplication()).msgRefund(payPwd, verifyStr, orderId, bz,
                new SimpleNetNavigator<MsgRefundBean>(snackbarText, dialogText, callback) {
                    @Override
                    public String getTitle() {
                        return "正在退款...";
                    }

                    @Override
                    public void success(MsgRefundBean data) {
                        //退款成功
                        if (data.getCode() == 10001) {
                            msgRefundBean.set(data.getData().get(0));
                            callback.success();
                        } else {
                            snackbarText.setValue(data.getMsg());
                        }
                    }

                    @Override
                    public void fail(String error) {
                        snackbarText.setValue(error);
                    }
                });
    }


    //验证订单是否超过退款期限
    public void verifyOrder(String orderId, Callback callback) {
        BillDataSource.getINSTANCE(getApplication()).verifyOrder(orderId,
                new SimpleNetNavigator<VerifyOrderBean>(snackbarText, dialogText, callback) {
                    @Override
                    public String getTitle() {
                        return "验证订单是否超过退款期限...";
                    }

                    @Override
                    public void success(VerifyOrderBean data) {

                        if (data.getCode() == 10001) {//成功后获取订单退款期限状态
                            orderStatus.set(data.getData().get(0).getStatus());
                            callback.success();
                        } else {
                            snackbarText.setValue(data.getMsg());
                        }

                    }

                    @Override
                    public void fail(String error) {
                        snackbarText.setValue(error);
                    }
                });

    }
}
